using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Linq;
using System.Linq;


using Soussous.Business;
using Soussous.DAO;
using Soussous.DAO.Connection;

namespace Soussous.DAO.Impl
{
    class OperationDAO : AbstractDAO<Operation>
    {
        private static OperationDAO INSTANCE = new OperationDAO();

        public static OperationDAO getInstance()
        {
            return INSTANCE;
        }

        private OperationDAO()
        {
        }
		
		public Table<Operation> getTable(){
			
			return OperationDAO.getInstance().getAll();
		
		}
        
        public List<Operation> getAllOperation() 
        {
            
            var operationList = from e in getTable()
                                select e;
            
            return operationList.ToList();
        }
		
		public List<Operation> searchByUserAndNote(int idUserFrom,int idUserTo,int idNote)
		{
            var operationList = from e in getTable()
                           where e.User_from == idUserFrom && e.User_to == idUserTo && e.Notes_id == idNote
                           select e;
			
			
			return operationList.ToList();
		}

        public List<Operation> searchUser(int idUserFrom) {

            var operationList = from e in getTable()
                                where e.User_from == idUserFrom
                                select e;


            return operationList.ToList();
        }
		
		public void updateByUserAndNote(int idUserFrom,int idUserTo,int idNote,Operation operation) 
        {
		
            Table<Operation> operations = getTable();
            var update = operations.First(e => e.Notes_id == idNote && e.User_from == idUserFrom && e.User_to == idUserTo);
            update.Amount = operation.Amount;
            operations.Context.SubmitChanges();

		}

        public void deleteOpeByUserAndNote(int idUserFrom, int idUserTo, int idNote)
        {
            Table<Operation> operations = getTable();
            var result = (from operation in operations
                          where operation.User_from == idUserFrom && operation.User_to == idUserTo && operation.Notes_id == idNote
                          select operation);
            operations.DeleteOnSubmit(result.Single());
            operations.Context.SubmitChanges();
        }

        public List<Operation> historical(int id) {

            var list = from e in getTable()
                       where e.User_to == id || e.User_from == id
                       select e;
            return list.ToList();
        
        }

        public IQueryable soldeById(int idUser) {

            var query1 = from income in getTable()
                         where income.User_from == idUser
                         group income.Amount by income.User_to into result
                         select new
                         {
                             Name = result.Key,
                             Somme = result.Sum()
                         };



            return query1;
	    } 
       
    }
}